package com.hbnu.iostream;

import java.io.IOException;
import java.io.PipedInputStream;

/**
 * @author kittychen
 * @since 2025-10-13 17:18
 */
public class Receive implements Runnable {

    private PipedInputStream pipedInputStream;

    public Receive() {
        this.pipedInputStream = new PipedInputStream();
    }

    public PipedInputStream getPipedInputStream() {
        return pipedInputStream;
    }

    @Override
    public void run() {
        try {
            byte[] bytes = new byte[1024];
            int len = 0;
            int temp;
            while ((temp = pipedInputStream.read()) != -1) {
                bytes[len++] = (byte) temp;
            }
            System.out.println(new String(bytes, 0, len));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pipedInputStream != null) {
                    pipedInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
